package com.api.gbhg.huangwm.action;

import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.huangwm.dao.RevenueBudgetDao;
import com.api.gbhg.liuwenbo.action.AmountVerification;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author	Huangwm
 * @Date 2021-12-03
 * @Version 1.0
 * @description	TODO 预算编制流程节点附件操作扣减/释放收入预算--用于三级单位和四级单位核批后实时扣减收入预算
 */
public class RevenueBudgetDeductionOther implements Action {
    private static RevenueBudgetDao budgetUtils = new RevenueBudgetDao();
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/action/RevenueBudgetDeductionOther",false);

    private static String YJ ;//科目级别
    private static String XJ ;//科目下级
    @Override
    public String execute(RequestInfo requestInfo) {
        logTool.writeLog("RevenueBudgetDeductionOther预算编制扣减收入预算Action workflowid="+requestInfo.getWorkflowid()+"  requestid="+requestInfo.getRequestid()+" tablename="+requestInfo.getRequestManager().getBillTableName());
        BaseBean baseBean = new BaseBean();
        YJ = baseBean.getPropValue("Verification","yj");
        XJ = baseBean.getPropValue("Verification","xj");
        logTool.writeLog("财政拨款核批层级 YJ="+YJ+"  XJ="+XJ);
        int mainTableId = VersionFlowUtil.getFlowTableLableId(requestInfo.getWorkflowid()+"");//获取formid 例如：-83
        String mainTabName = AmountVerification.getMainTableName(mainTableId);//获取主表名
        Integer dw = AmountVerification.getwofkflowDw(mainTabName, requestInfo.getRequestid()+"");
        List<String> flowDetailTableNames = AmountVerification.getDetailTables(mainTableId);//获取明细表名称
        String flowTableName = VersionFlowUtil.getFlowTableName(requestInfo.getWorkflowid()+"");//获取表名
        String mainid = AmountVerification.getIdByTable(flowTableName, requestInfo.getRequestid()+""); //获取流程当前id
        //System.err.println(mainid+" 核批明细 "+flowDetailTableNames);

        List<Map<String,String>> getSNDatas = new ArrayList<>();//上年结转
        List<Map<String,String>> getDNDatas = new ArrayList<>();//当年预算
        String ysnd = "";
        try {
            ysnd = budgetUtils.getFieldValue("ysnd", flowTableName, requestInfo.getRequestid()+"");
            if(ysnd.isEmpty()){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("获取预算年度为空");
                return  FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("获取预算年度异常 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }
        try {
            getSNDatas = budgetUtils.getSNDatas(flowDetailTableNames.get(1),mainid);//上年结转
            getDNDatas = budgetUtils.getDNDatas(flowDetailTableNames.get(0),mainid);//当年预算
            logTool.writeLog(" 上年结转 "+getSNDatas);
            logTool.writeLog(" 当年预算 "+getDNDatas);
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("获取上年结转和当年预算核批数据异常 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }

        Map<String, Object> result = budgetUtils.RevenueBudgetDeduction(getSNDatas,getDNDatas,dw,ysnd);
        logTool.writeLog("扣减结果："+ JSONObject.toJSONString(result));
        if(!result.get("code").equals("1")){
            String f1 = result.get("f1").toString();
            String f2 = result.get("f2").toString();
            String f3 = result.get("f3").toString();
            String f4 = result.get("f4").toString();
            String f5 = result.get("f5").toString();
            String f6 = result.get("f6").toString();
            String f7 = result.get("f7").toString();
            String f8 = result.get("f8").toString();
            String msg = "<br />"+f1+f2+f3+f4+f5+f6+f7+f8;
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("扣减收入预算金额失败 "+msg);
            return  FAILURE_AND_CONTINUE;
        }else{
            //扣减成功 记录核批数据
            String xmmc = budgetUtils.getFieldValue("xmmc", flowTableName, requestInfo.getRequestid() + "");
            String sqze = budgetUtils.getFieldValue("sqzey", flowTableName, requestInfo.getRequestid() + "");
            String hpze = budgetUtils.getFieldValue("hpze", flowTableName, requestInfo.getRequestid() + "");
            Map<String, String> mainData = new HashMap<>();
            mainData.put("lcid", requestInfo.getRequestid() + "");
            mainData.put("ysnd", ysnd);
            mainData.put("xmmc", xmmc);
            mainData.put("sqze", sqze);
            mainData.put("hpze", hpze);
            mainData.put("version", "other");
            Map<String, Object> result3 = budgetUtils.setPeojectApprovalRecords(mainData, getSNDatas, getDNDatas);
            if (!result3.get("code").equals("1")) {
                boolean f1 = (boolean) result3.get("f1");
                boolean f2 = (boolean) result3.get("f2");
                boolean f3 = (boolean) result3.get("f3");
                String msg = "";
                if (!f1) {
                    msg += "插入主表数据SQL执行异常;";
                }
                if (!f2) {
                    msg += "插入上年结转核批数据SQL执行异常;";
                }
                if (!f3) {
                    msg += "插入当年预算核批数据SQL执行异常;";
                }
                logTool.writeLog("存档核批数据异常 msg=" + msg);
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("存档核批数据异常>" + msg);
                return FAILURE_AND_CONTINUE;
            }
        }
        RecordSet rs = new RecordSet();
        rs.execute("update "+requestInfo.getRequestManager().getBillTableName() +" set estjfgld=1 where requestid="+requestInfo.getRequestid());
        WorkflowComInfo comInfo = new WorkflowComInfo();
        comInfo.removeWorkflowCache();
        return SUCCESS;
    }
}
